package com.mooc.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.mooc.entity.Course;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

/**
 * 文件上传包装类
 * jpg文件与其他文件分开存放
 * @author ccnoobs-wyf
 *
 */
public class UploadFile {
	// 上传文件存储目录
	private static final String UPLOAD_DIRECTORY = "video";
	//上传图片存放位置
	private static final String UPLOADImage_DIRECTORY = "pic";
	//上传音乐存放位置
	private static final String UPLOADMUSIC_DIRECTORY = "music";

	// 上传配置
	private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
	private static final int MAX_FILE_SIZE      = 1024 * 1024 * 500; // 500MB
	private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 500; // 500MB
	/**
	 * 方法uploadFile("保存的文件名",HttpServletRequest,HttpServletResponse)
	 * @param refilename
	 * @param request
	 *
	 * @return
	 * @return
	 */
	public static <T> T uploadFile(String refilename,HttpServletRequest request,Class<T> c){
			/*//程序状态
			boolean isok = true;*/
		// 检测是否为多媒体上传
		if (!ServletFileUpload.isMultipartContent(request)) {
			// 如果不是则停止
			System.err.println("Error: 表单必须包含 enctype=multipart/form-data");
	          /*  PrintWriter writer = response.getWriter();
	            writer.println("Error: 表单必须包含 enctype=multipart/form-data");
	            writer.flush();*/
			return null;
		}

		// 配置上传参数
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
		factory.setSizeThreshold(MEMORY_THRESHOLD);
		// 设置临时存储目录
		factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

		ServletFileUpload upload = new ServletFileUpload(factory);

		// 设置最大文件上传值
		upload.setFileSizeMax(MAX_FILE_SIZE);

		// 设置最大请求值 (包含文件和表单数据)
		upload.setSizeMax(MAX_REQUEST_SIZE);

		// 中文处理
		upload.setHeaderEncoding("UTF-8");

		String os = System.getProperty("os.name");
		String pathAbsolute="D:/mooc";
		if (os.toLowerCase().startsWith("linux")){
			pathAbsolute="/usr/mooc";
		}

		// 构造临时路径来存储上传的文件
		// 这个路径相对当前应用的目录
		String uploadPath = pathAbsolute + File.separator + UPLOAD_DIRECTORY;
		String uploadImagePath = pathAbsolute + File.separator + UPLOADImage_DIRECTORY;
		String uploadMusicPath = pathAbsolute + File.separator + UPLOADMUSIC_DIRECTORY;


		// 如果目录不存在则创建
		File uploadDir = new File(uploadPath);
		File uploadImageDir = new File(uploadImagePath);
		File uploadMuiscDir = new File(uploadMusicPath);
		if (!uploadDir.exists()) {
			uploadDir.mkdir();
		}
		if (!uploadImageDir.exists()) {
			uploadImageDir.mkdir();
		}
		if (!uploadMuiscDir.exists()) {
			uploadMuiscDir.mkdir();
		}
		Map<String,String> pmap = new HashMap<>();
		T t = null;


		boolean isUpdate = false;
		try {
			// 解析请求的内容提取文件数据
			@SuppressWarnings("unchecked")
			List<FileItem> formItems = upload.parseRequest(new ServletRequestContext(request));

			if (formItems != null && formItems.size() > 0) {
				// 迭代表单数据
				for (FileItem item : formItems) {
					// 处理在表单中的字段
					if(item.isFormField()) {
						pmap.put(item.getFieldName(), item.getString("utf-8"));
					}

				}
                String id = pmap.get("id");
                if(id!=null&& !"".equals(id)) {
					refilename = pmap.get("id");
					isUpdate = true;
					/*System.out.println(pmap.get("id"));*/
				}
				for (FileItem item : formItems) {
					// 处理不在表单中的字段
					if (!item.isFormField()) {
						String fileName = new File(item.getName()).getName();
						String fileExtName = fileName.substring(fileName.lastIndexOf(".")+1);
						String filePath = uploadPath + File.separator + refilename+"."+fileExtName;
						if(fileExtName.equals("jpg")) {
							filePath = uploadImagePath + File.separator + refilename+"."+fileExtName;
						}else if(fileExtName.equals("mp3")){
							filePath = uploadMusicPath  + File.separator + refilename+"."+fileExtName;
						}
						File storeFile = new File(filePath);
						// 在控制台输出文件的上传路径
						System.out.println(filePath);
						// 保存文件到硬盘
						item.write(storeFile);
						System.out.println("文件上传成功!");
	                        /*request.setAttribute("message",
	                            "文件上传成功!");*/
					}
				}
			}
		} catch (Exception ex) {
			System.err.println( "错误信息: " + ex.getMessage());
	           /* request.setAttribute("message",
	                    "错误信息: " + ex.getMessage());*/
		}



		try {
			t = c.getConstructor(Long.class, String.class,String.class,String.class,String.class,String.class,String.class)
					.newInstance(isUpdate?Long.parseLong(refilename):null,
					pmap.get("name"),
					pmap.get("context"),
					pmap.get("type"),
					"1",
					UPLOADImage_DIRECTORY+"/"+refilename+".jpg",
					refilename);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
//		course.setId();
//		course.setName(pmap.get("name"));
//		course.setContext(pmap.get("context"));
//		course.setType(pmap.get("type"));
//		course.setPrice("1");
//		course.setPic(UPLOADImage_DIRECTORY+"/"+refilename+".jpg");
//		course.setVideo(UPLOAD_DIRECTORY+"/"+refilename);
		return t;
	}
}
